package com.snda.lidejia.javatest;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.apache.log4j.Logger;

/**
 * Hello world!
 *
 */
public class App {

    private static Logger LOGGER = Logger.getLogger(App.class);
    //
    private static int SIZE = 50000000;
    private static final Random RANDOM = new Random();

    private static void testInsertAndSequenceRead(List<Integer> numbers) {
        int sum = 0;
        for (int i = 0; i < SIZE; i++) {
            numbers.add(RANDOM.nextInt());
        }
        for (Integer number : numbers) {
            sum += number;
        }
    }

    public static void main(String[] args) throws Exception {
        if (args.length == 1) {
            SIZE = Integer.parseInt(args[0]);
        }
        long startTime = 0;
        //
        List<Integer> numbers = new ArrayList<Integer>();
        startTime = System.currentTimeMillis();
        testInsertAndSequenceRead(numbers);
        LOGGER.info(String.format("ArrayList: %s ms", System.currentTimeMillis() - startTime));
        System.gc();
        numbers = new LinkedList<Integer>();
        startTime = System.currentTimeMillis();
        testInsertAndSequenceRead(numbers);
        LOGGER.info(String.format("LinkedList: %s ms", System.currentTimeMillis() - startTime));
    }
}
